<template>
  <div>
    <BookInfo :info='info'></BookInfo>
    <CommentList :comments='comments'></CommentList>
    <div class="comment"
      v-if='showAdd'>
      <textarea placeholder="请输入图书短评"
        :maxlength="100"
        class="textarea"
        v-model="comment"></textarea>
      <div class="location">
        地理位置：
        <switch color='#ea5a49'
          :checked='location'
          @change="getGeo"></switch>
        <span class="text-primary">{{location}}</span>
      </div>
      <div class="phone">
        手机型号：
        <switch color='#ea5a49'
          :checked='phone'
          @change="getPhone"></switch>
        <span class="text-primary">{{phone}}</span>
      </div>
      <button class="btn"
        @click="addComment">
        评论
      </button>
    </div>
    <div v-else
      class='text-footer'>
      未登录或者已经评论过
    </div>
    <button class="btn" open-type='share'>转发给好友</button>
  </div>
</template>

<script>
import { get, post, showModal } from '@/util'
import BookInfo from '@/components/BookInfo'
import CommentList from '@/components/CommentList'
export default {
  data () {
    return {
      userinfo: {},
      bookid: '',
      info: {},
      comment: '',
      location: '',
      phone: '',
      comments: []
    }
  },
  computed: {
    showAdd () {
      if (!this.userinfo.openId) {
        return false
      }
      if (this.comments.filter(v => v.openid === this.userinfo.openId).length) {
        return false
      }
      return true
    }
  },
  components: {
    BookInfo,
    CommentList
  },
  methods: {
    async addComment () {
      if (!this.comment) {
        showModal('请输入短评')
        return
      }
      // 评论内容 手机型号 地理位置 图书ID 用户openID
      const data = {
        openid: this.userinfo.openId,
        bookid: this.bookid,
        comment: this.comment,
        phone: this.phone,
        location: this.location
      }
      try {
        await post('/weapp/addcomment', data)
        this.comment = ''
        this.getComments()
      } catch (e) {
        showModal('评论失败', e.msg)
      }
    },
    async getDetail () {
      wx.showLoading()
      const info = await get('/weapp/bookdetail', { id: this.bookid })

      wx.setNavigationBarTitle({
        title: info.title
      })
      this.info = info
      wx.hideLoading()
    },
    async getComments () {
      const comments = await get('/weapp/commentlist', { bookid: this.bookid })
      this.comments = comments.list || []
    },
    getGeo (e) {
      if (e.target.value) {
        const ak = 'jSToDABK7RcekxXYQ368ciDY1ksnqEgN'
        const url = 'http://api.map.baidu.com/geocoder/v2/'
        wx.getLocation({
          success: (geo) => {
            wx.request({
              url,
              data: {
                ak,
                location: `${geo.latitude},${geo.longitude}`,
                output: 'json'
              },
              success: res => {
                if (res.data.status === 0) {
                  this.location = res.data.result.addressComponent.city
                } else {
                  this.location = '未知地址'
                }
              }
            })
          }
        })
      } else {
        this.location = ''
      }
    },
    getPhone (e) {
      if (e.target.value) {
        const phoneInfo = wx.getSystemInfoSync()
        this.phone = phoneInfo.model
      } else {
        this.phone = ''
      }
    }
  },
  mounted () {
    this.bookid = this.$root.$mp.query.id
    this.getDetail()
    this.getComments()
    const userinfo = wx.getStorageSync('userInfo')
    if (userinfo) {
      this.userinfo = userinfo
    }
  },
  onShareAppMessage () {
    return {
      title: '书籍：' + this.info.title,
      desc: '作者：' + this.info.author,
      path: '/page/detail?id=' + this.bookid
    }
  }
}
</script>

<style lang='scss'>
.comment {
  padding: 10px;
  .textarea {
    box-sizing: border-box;
    width: 100%;
    background: #eee;
    padding: 10px;
    border-radius: 5px;
  }
  .location {
    margin-top: 10px;
    padding: 5px 10px;
  }
  .phone {
    margin-top: 10px;
    padding: 5px 10px;
  }
}
</style>
